package com.lvchen.ui.fourfunction;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.lvchen.R;
import com.lvchen.common.base.AppManager;
import com.lvchen.common.base.BaseActivity;
import com.lvchen.common.utils.UIHelper;
import com.lvchen.ui.fourfunction.widget.PoolBallView;
import com.mxn.soul.flowingdrawer_core.ElasticDrawer;
import com.mxn.soul.flowingdrawer_core.FlowingDrawer;
import com.siberiadante.titlelayoutlib.TitleBarLayout;

/**
 * 例子页
 */
public class GravityActivity extends BaseActivity {

    private FlowingDrawer fd_home;

    private TitleBarLayout tb_title;

    private PoolBallView pb_gravity;

    private Sensor mDefaultSensor;
    private SensorManager mSensorManager;
    private int[] imgs = {R.mipmap.img_01, R.mipmap.img_02, R.mipmap.img_03, R.mipmap.img_04, R.mipmap.img_05};

    private SensorEventListener listerner = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                int x = (int) (event.values[0]);
                int y = (int) (event.values[1]*2.0f);

                if(x>0&&y>0){
                    pb_gravity.getBallView().rockBallByImpulse(-2, 2);
                }else if(x>0&&y<0){
                    pb_gravity.getBallView().rockBallByImpulse(-2, -2);
                }else if(x<0&&y>0){
                    pb_gravity.getBallView().rockBallByImpulse(2, 2);
                }else if(x<0&&y<0){
                    pb_gravity.getBallView().rockBallByImpulse(2, -2);
                }else if(x==0&&y==0){
                    pb_gravity.getBallView().rockBallByImpulse(0, 0);
                }else if(x==0&&y>0){
                    pb_gravity.getBallView().rockBallByImpulse(0, 2);
                }else if(x==0&&y<0){
                    pb_gravity.getBallView().rockBallByImpulse(0, -2);
                }else if(x<0&&y==0){
                    pb_gravity.getBallView().rockBallByImpulse(2, 0);
                }else if(x>0&&y==0){
                    pb_gravity.getBallView().rockBallByImpulse(-2, 0);
                }

//                if (lastX != x || lastY != y) {//防止频繁回调,画面抖动
//                    pb_gravity.getBallView().rockBallByImpulse(-x, y);
//                }
                
                lastX = x;
                lastY = y;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    private int lastX;
    private int lastY;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gravity);
        ensureUi();
    }

    private void ensureUi() {

        fd_home = (FlowingDrawer) findViewById(R.id.fd_home);
        pb_gravity = (PoolBallView) findViewById(R.id.pb_gravity);
        tb_title = (TitleBarLayout) findViewById(R.id.tb_title);
        tb_title.setIsHaveLine(false);

        tb_title.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity();
            }
        });

        fd_home.setMaxAnimationDuration(2000);

        fd_home.setTouchMode(ElasticDrawer.TOUCH_MODE_BEZEL);

        fd_home.setOnDrawerStateChangeListener(new ElasticDrawer.OnDrawerStateChangeListener() {
            @Override
            public void onDrawerStateChange(int oldState, int newState) {
                if (newState == ElasticDrawer.STATE_CLOSED) {

                }
            }

            @Override
            public void onDrawerSlide(float openRatio, int offsetPixels) {

            }
        });

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mDefaultSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        init();

    }

    private void init() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        for (int i = 0; i < imgs.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imgs[i]);
            imageView.setTag(R.id.circle_tag, true);
            pb_gravity.addView(imageView, layoutParams);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(GravityActivity.this, "点击了气泡", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        pb_gravity.getBallView().onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        pb_gravity.getBallView().onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(listerner, mDefaultSensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(listerner);
    }


    public static void goToThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, GravityActivity.class);
        context.startActivity(intent);
    }

}
